/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.edu.agh.administracja.networkconfig.view;

import java.io.File;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;

/**
 * Przy pomocy tego okna dialogowego mozna pobrac nazwe wybranej konfiguracji.
 * @author piotrek
 */
public class SelectConfigurationDialog extends javax.swing.JDialog {

    public static final String CONFIG_FOLDER = "interfaces_conf";
    
    private String selectedConfiguration = null;
    
    public static String getConfigName(java.awt.Frame parent){
        
        SelectConfigurationDialog dialog = new SelectConfigurationDialog(parent, true);
        dialog.setVisible(true);
        
        return dialog.selectedConfiguration;
    }
    
    private SelectConfigurationDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        titlePanel = new javax.swing.JPanel();
        titleLabel = new javax.swing.JLabel();
        contentPanel = new javax.swing.JPanel();
        comboPanel = new javax.swing.JPanel();
        configNameLabel = new javax.swing.JLabel();
        configNameCombo = new javax.swing.JComboBox();
        buttonsPanel = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        titleLabel.setFont(new java.awt.Font("DejaVu Sans", 1, 14)); // NOI18N
        titleLabel.setText("Wybierz konfigurację");
        titlePanel.add(titleLabel);

        getContentPane().add(titlePanel, java.awt.BorderLayout.PAGE_START);

        contentPanel.setLayout(new javax.swing.BoxLayout(contentPanel, javax.swing.BoxLayout.Y_AXIS));

        configNameLabel.setText("Konfiguracja");
        comboPanel.add(configNameLabel);

        configNameCombo.setModel(getComboModel());
        configNameCombo.setPreferredSize(new java.awt.Dimension(300, 28));
        comboPanel.add(configNameCombo);

        contentPanel.add(comboPanel);

        buttonsPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        okButton.setText("Ok");
        okButton.setPreferredSize(new java.awt.Dimension(80, 30));
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        buttonsPanel.add(okButton);

        cancelButton.setText("Cancel");
        cancelButton.setPreferredSize(new java.awt.Dimension(80, 30));
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        buttonsPanel.add(cancelButton);

        contentPanel.add(buttonsPanel);

        getContentPane().add(contentPanel, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        selectedConfiguration = (String) configNameCombo.getSelectedItem();
        setVisible(false);
    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel buttonsPanel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JPanel comboPanel;
    private javax.swing.JComboBox configNameCombo;
    private javax.swing.JLabel configNameLabel;
    private javax.swing.JPanel contentPanel;
    private javax.swing.JButton okButton;
    private javax.swing.JLabel titleLabel;
    private javax.swing.JPanel titlePanel;
    // End of variables declaration//GEN-END:variables

    private ComboBoxModel getComboModel(){
        
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        
        File dir = new File(CONFIG_FOLDER);
        
        for(File file : dir.listFiles())
        {
            if(file.isFile())
                model.addElement(file.getName());
        }
        
        return model;
        
    }

}
